﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.ComponentModel;
using System.Drawing;

namespace Noris.Tools.TraceVisualiser.Components
{
	internal class BigButton : Button
	{
		public BigButton()
		{
			this.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
			this.FlatAppearance.BorderSize = 0;
			this.UseVisualStyleBackColor = true;

			this.Enter += new EventHandler(BigButton_Enter);
			this.Leave += new EventHandler(BigButton_Leave);
			this.MouseEnter += new EventHandler(BigButton_MouseEnter);
			this.MouseLeave += new EventHandler(BigButton_MouseLeave);
		}
		void BigButton_Enter(object sender, EventArgs e)
		{
			this._HasFocus = true;
			this.SetActive();
		}
		void BigButton_Leave(object sender, EventArgs e)
		{
			this._HasFocus = false;
			this.SetInactive();
		}
		void BigButton_MouseEnter(object sender, EventArgs e)
		{
			this._HasMouse = true;
			this.SetActive();
		}
		void BigButton_MouseLeave(object sender, EventArgs e)
		{
			this._HasMouse = false;
			this.SetInactive();
		}
		private void _ShowHelp(bool show)
		{
			if (this.HelpControl != null && !String.IsNullOrEmpty(this.HelpText))
			{
				if (show)
					this.HelpControl.Text = this.HelpText;
				else if (!this.LeaveTextOnExit)
					this.HelpControl.Text = "";
				this.HelpControl.BackColor = System.Drawing.SystemColors.Info;
			}
		}
		[Category("BigButton")]
		[Description("Objekt, do jehož property Text se bude promítat hodnota this.HelpText")]
		public Control HelpControl { get; set; }
		[Category("BigButton")]
		[Description("Text, který se zobrazí v objektu this.HelpControl v době aktivity tohoto buttonu")]
		[Bindable(true)]
		public string HelpText { get; set; }
        [Category("BigButton")]
        [Description("Ponechávat text (this.HelpText) zobrazený v objektu this.HelpControl i po odchodu z this. Default = false.")]
        public bool LeaveTextOnExit { get; set; }
        [Description("Obrázek, který se bude zobrazovat jako aktivní")]
		[Category("BigButton")]
		public Image ImageActive { get; set; }
		[Description("Obrázek, který se bude zobrazovat jako neaktivní. Pokud je null, pak při přechodu do aktivního stavu se sem uloží Image.")]
		[Category("BigButton")]
		public Image ImageInActive { get; set; }
		[Browsable(false)]
		[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
		public bool StillActive { get { return _StillActive; } set { _SetStillActive(value); } }

		private void _SetStillActive(bool value)
		{
			this._StillActive = value;
			if (value || this._HasMouse)
				this._SetActiveImage();
			else
				this._SetInActiveImage();
		}
		private bool _StillActive;
		private void SetActive()
		{
			this._SetActiveImage();
			this._ShowHelp(true);
			this.FlatStyle = System.Windows.Forms.FlatStyle.Standard;
		}
		private void SetInactive()
		{
			this.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
			this._SetInActiveImage();
			this._ShowHelp(false);
		}
		private void _SetActiveImage()
		{
			if (this.ImageInActive == null)
				this.ImageInActive = this.Image;

			if (this.ImageActive != null)
				this.Image = this.ImageActive;
		}
		private void _SetInActiveImage()
		{
			if (this.ImageInActive != null && !this.StillActive)
				this.Image = this.ImageInActive;
		}
		private bool _HasMouse;
		private bool _HasFocus;
	}
}
